version 14.2
set more off
set matsize 11000
set seed 12345
clear all
clear mata

***********************************************************************************************************************
* THIS ROUTINE GENERATES TABLE III OF WASEEM (2020) ROLE OF WITHHOLDING ...
***********************************************************************************************************************

/* TABLE */

#d				;
matrix 			input table=(0,0,0,0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0,0,0,0\
							 0,0,0,0,0,0,0,0,0,0,0,0\0,0,0,0,0,0,0,0,0,0,0,0);
#d				cr


/* REGISTRATION */
*-----------------

clear					mata
use 			 		"$project_data\Master Short Updated Nov 2011.dta", clear
keep                	if business_sts=="1" | business_sts=="2"
g						ind=real(substr(item_cd,1,4))
drop					if ind<1600
drop					if ind>=2500 & ind<2800
keep					regno business_sts item_cd date_comm*
g						rmonth=mofd(date_comm)
g						treat=business_sts=="1"
g						one=1
bys						rmonth treat:egen nregism=sum(one)
keep					if date_comm>=d(01may1999) & date_comm<d(01may2005)
duplicates				drop rmonth treat, force
g						period=rmonth
qui						sum period
replace					period=period-r(min)+1
keep					rmonth treat nregism period 
reshape					wide nregism, i(period) j(treat)
ren						nregism0 nimport
ren						nregism1 nmanuf
g						diff=nmanuf-nimport
g						percentdiff=diff/nimport
g						diff_j=diff
g 						date=dofm(rmonth)
replace					diff_j=0 if date<d(01jan2002)

/* BOOTSTRAP FOR STANDARD ERROR */

mata
								nmanuf=st_data(.,"nmanuf")
								nimport=st_data(.,"nimport")
								diff_j=st_data(.,"diff_j")
								m=length(nmanuf)
								nboot=100
								b_boot=J(m,nboot,0)
								se=J(m,1,0)
								for (j=1; j<=nboot; j++)
								{
									error_j=jumble(diff_j)
									nmanufc_j=nmanuf+error_j
									absdiff_j=(nmanufc_j:-nimport)
									b_boot[,j]=absdiff_j
								}
								for (i=1; i<=m; i++)
								{
									se[i]=sqrt(quadvariance(b_boot[i,]'))
								}
								st_matrix("se",se)
end
svmat 					se, names(se)
g						ey=0
local					y=1999
forvalues				p=1(12)72 {
replace					ey=`y' if period >=`p' & period<`p'+12
local					y=`y'+1
}
bys						ey:egen diff_ey=sum(diff)
g						se_ey=0
local					fp=1
forvalues				y=1999/2004 {
local					var_p=0
local					lp=`fp'+11
forvalues				p=`fp'/`lp' {
local					var_p=`var_p'+(se1[`p']^2)
}
local					se_ey=sqrt(`var_p')
replace					se_ey=`se_ey' if ey==`y'
local					fp=`fp'+12
}
bys						ey:egen nimport_ey=sum(nimport)
bys						ey:egen nmanuf_ey=sum(nmanuf)
replace					diff_ey=diff_ey/nimport_ey
replace					se_ey=se_ey/nimport_ey
duplicates				drop ey, force
local					i=1
forvalues				y=1/6 {
local					j=1
matrix					table[`i',`j']=ey[`y']
local					j=`j'+1
matrix					table[`i',`j']=nmanuf_ey[`y']
local					j=`j'+1
matrix					table[`i',`j']=nimport_ey[`y']
local					j=`j'+1
matrix					table[`i',`j']=diff_ey[`y']
local					i=`i'+1
matrix					table[`i',`j']=se_ey[`y']
local					i=`i'+1
}


/* ENTRY */
*------------

clear					mata
use						"$project_data\STPAK_1996_2011_UBP_v1.dta", clear
keep                	if business_sts=="1" | business_sts=="2"
bys						regno:egen entry=min(taxperiod)	
duplicates				drop regno, force
keep					entry taxperiod fy item_cd business_sts
g						ind=real(substr(item_cd,1,4))
drop					if ind<1600
drop					if ind>=2500 & ind<2800
g						treat=(business_sts=="1")
g						one=1
bys						entry treat:egen nentrants=sum(one)
g						period=mofd(entry)
keep					if entry>=d(01may1999) & entry<d(01may2005)
qui						sum period
replace					period=period-r(min)+1
duplicates				drop entry treat, force
drop					business_sts item_cd ind one
reshape					wide nentrants, i(period) j(treat)
ren						nentrants0 nimport
ren						nentrants1 nmanuf
g						diff=nmanuf-nimport
g						percentdiff=diff/nimport
g						diff_j=diff
replace					diff_j=0 if entry<d(01jan2002)

/* BOOTSTRAP FOR STANDARD ERROR */

mata
								nmanuf=st_data(.,"nmanuf")
								nimport=st_data(.,"nimport")
								diff_j=st_data(.,"diff_j")
								m=length(nmanuf)
								nboot=100
								b_boot=J(m,nboot,0)
								se=J(m,1,0)
								for (j=1; j<=nboot; j++)
								{
									error_j=jumble(diff_j)
									nmanufc_j=nmanuf+error_j
									absdiff_j=(nmanufc_j:-nimport)
									b_boot[,j]=absdiff_j
								}
								for (i=1; i<=m; i++)
								{
									se[i]=sqrt(quadvariance(b_boot[i,]'))
								}
								st_matrix("se",se)
end
svmat 					se, names(se)
g						ey=0
local					y=1999
forvalues				p=1(12)72 {
replace					ey=`y' if period >=`p' & period<`p'+12
local					y=`y'+1
}
bys						ey:egen diff_ey=sum(diff)
g						se_ey=0
local					fp=1
forvalues				y=1999/2004 {
local					var_p=0
local					lp=`fp'+11
forvalues				p=`fp'/`lp' {
local					var_p=`var_p'+(se1[`p']^2)
}
local					se_ey=sqrt(`var_p')
replace					se_ey=`se_ey' if ey==`y'
local					fp=`fp'+12
}
bys						ey:egen nimport_ey=sum(nimport)
bys						ey:egen nmanuf_ey=sum(nmanuf)
replace					diff_ey=diff_ey/nimport_ey
replace					se_ey=se_ey/nimport_ey
duplicates				drop ey, force
local					i=1
forvalues				y=1/6 {
local					j=5
matrix					table[`i',`j']=nmanuf_ey[`y']
local					j=`j'+1
matrix					table[`i',`j']=nimport_ey[`y']
local					j=`j'+1
matrix					table[`i',`j']=diff_ey[`y']
local					i=`i'+1
matrix					table[`i',`j']=se_ey[`y']
local					i=`i'+1
}

/* REAL ENTRY */
*---------------

clear					mata
use						"$project_data\STPAK_1996_2011_UBP_v1.dta", clear
keep                	if business_sts=="1" | business_sts=="2"
g						active=(sale_ag>0 | purch_ag>0 | output>0 | input>0)
keep					if active==1
bys						regno:egen entry=min(taxperiod)	
duplicates				drop regno, force
keep					entry taxperiod fy item_cd business_sts
g						ind=real(substr(item_cd,1,4))
drop					if ind<1600
drop					if ind>=2500 & ind<2800
g						treat=(business_sts=="1")
g						one=1
bys						entry treat:egen nentrants=sum(one)
g						period=mofd(entry)
keep					if entry>=d(01may1999) & entry<d(01may2005)
qui						sum period
replace					period=period-r(min)+1
duplicates				drop entry treat, force
drop					business_sts item_cd ind one
reshape					wide nentrants, i(period) j(treat)
ren						nentrants0 nimport
ren						nentrants1 nmanuf
g						diff=nmanuf-nimport
g						percentdiff=diff/nimport
g						diff_j=diff
replace					diff_j=0 if entry<d(01jan2002)

/* BOOTSTRAP FOR STANDARD ERROR */

mata
								nmanuf=st_data(.,"nmanuf")
								nimport=st_data(.,"nimport")
								diff_j=st_data(.,"diff_j")
								m=length(nmanuf)
								nboot=100
								b_boot=J(m,nboot,0)
								se=J(m,1,0)
								for (j=1; j<=nboot; j++)
								{
									error_j=jumble(diff_j)
									nmanufc_j=nmanuf+error_j
									absdiff_j=(nmanufc_j:-nimport)
									b_boot[,j]=absdiff_j
								}
								for (i=1; i<=m; i++)
								{
									se[i]=sqrt(quadvariance(b_boot[i,]'))
								}
								st_matrix("se",se)
end
svmat 					se, names(se)
g						ey=0
local					y=1999
forvalues				p=1(12)72 {
replace					ey=`y' if period >=`p' & period<`p'+12
local					y=`y'+1
}
bys						ey:egen diff_ey=sum(diff)
g						se_ey=0
local					fp=1
forvalues				y=1999/2004 {
local					var_p=0
local					lp=`fp'+11
forvalues				p=`fp'/`lp' {
local					var_p=`var_p'+(se1[`p']^2)
}
local					se_ey=sqrt(`var_p')
replace					se_ey=`se_ey' if ey==`y'
local					fp=`fp'+12
}
bys						ey:egen nimport_ey=sum(nimport)
bys						ey:egen nmanuf_ey=sum(nmanuf)
replace					diff_ey=diff_ey/nimport_ey
replace					se_ey=se_ey/nimport_ey
duplicates				drop ey, force
local					i=1
forvalues				y=1/6 {
local					j=8
matrix					table[`i',`j']=nmanuf_ey[`y']
local					j=`j'+1
matrix					table[`i',`j']=nimport_ey[`y']
local					j=`j'+1
matrix					table[`i',`j']=diff_ey[`y']
local					i=`i'+1
matrix					table[`i',`j']=se_ey[`y']
local					i=`i'+1
}

/* EXPORTING THE TABLE */ 

svmat 			table
keep			tab*
keep			if _n<=12
drop			table11-table12
forvalues 		c=1/10 {
g				c`c'=string(table`c',"%9.3f")
}
replace			c1=string(table1,"%9.0f")
forvalues		r=2(2)12 {
replace			c1="" in `r'
}
foreach			c in 2 3 5 6 8 9 {
forvalues		r=1(2)11 {
local			r1=`r'+1
replace			c`c'=string(table`c',"%9.0fc") in `r'
replace			c`c'="" in `r1'
}
}
foreach			c in 4 7 10 {
forvalues		r=2(2)12 {
replace			c`c'="("+string(table`c',"%9.3f")+")" in `r'
}
}
forvalues		s=1/15 {
g				s`s'="&"
}
g				l="\\"
order			c1 s1 c2 s2 c3 s3 c4 s4 s14 c5 s5 c6 s6 c7 s7 s13 c8 s8  c9 s9 c10 l
browse			c1 s1 c2 s2 c3 s3 c4 s4 s14 c5 s5 c6 s6 c7 s7 s13 c8 s8  c9 s9 c10 l

